package com.factory.test.activity;

import android.media.AudioManager;
import android.media.Ringtone;
import android.media.RingtoneManager;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.SeekBar;

import androidx.annotation.Nullable;
import androidx.databinding.DataBindingUtil;

import com.factory.test.base.BaseTestActivity;
import com.factory.test.R;
import com.factory.test.databinding.ActivitySoundPlayBinding;


/**
 * 声音测试(播放声音) 喇叭测试
 */
public class SoundPlayActivity extends BaseTestActivity {

    private ActivitySoundPlayBinding binding;
    private AudioManager mAudioManager;
    private int mMaxVolume;
    private int mNowVolume;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = DataBindingUtil.setContentView(this, R.layout.activity_sound_play);
        mAudioManager = (AudioManager) getSystemService(AUDIO_SERVICE);
        mMaxVolume = mAudioManager.getStreamMaxVolume(AudioManager.STREAM_MUSIC);
        mNowVolume = mAudioManager.getStreamVolume(AudioManager.STREAM_MUSIC);

        binding.value.setText(String.format("当前音量: %d", mNowVolume));

        binding.seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            @Override
            public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
                if(progress <= 0){
                    progress = 1;
                }
                mMaxVolume = mAudioManager.getStreamMaxVolume(AudioManager.STREAM_MUSIC);
                mNowVolume = getVolume(progress, mMaxVolume);
                mAudioManager.setStreamVolume(AudioManager.STREAM_MUSIC, mNowVolume, AudioManager.FLAG_PLAY_SOUND);

                mMaxVolume = mAudioManager.getStreamMaxVolume(AudioManager.STREAM_ALARM);
                mNowVolume = getVolume(progress, mMaxVolume);
                mAudioManager.setStreamVolume(AudioManager.STREAM_ALARM, mNowVolume, AudioManager.FLAG_PLAY_SOUND);

                mMaxVolume = mAudioManager.getStreamMaxVolume(AudioManager.STREAM_RING);
                mNowVolume = getVolume(progress, mMaxVolume);
                mAudioManager.setStreamVolume(AudioManager.STREAM_RING, mNowVolume, AudioManager.FLAG_PLAY_SOUND);

                binding.value.setText(String.format("当前音量: %d", progress));
            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {

            }

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {

            }
        });
        binding.seekBar.setProgress(binding.seekBar.getMax() * mNowVolume / mMaxVolume);

        binding.open.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startPlay();
            }
        });
    }

    public static int getVolume(float current, float max){
        int v = Math.round(((current * max) / 100));
        if (v <= 0) {
            return 1;
        }
        return v;
    }

    private Ringtone ringtone;

    public void defaultCallMediaPlayer(){
        if(ringtone != null && ringtone.isPlaying()){
            ringtone.stop();
        }
        try {
            Uri defaultRingtoneUri = RingtoneManager.getActualDefaultRingtoneUri(context, RingtoneManager.TYPE_RINGTONE);
            ringtone = RingtoneManager.getRingtone(context, defaultRingtoneUri);

//          RingtoneManager ringtoneManager= new RingtoneManager(context); // 铃声管理器
//          ringtone = ringtoneManager.getRingtone(0);

            ringtone.setLooping(true);
        } catch (Exception e) {
            //e.printStackTrace();
        }
        ringtone.play();
    }


    private void startPlay() {
        try {
            defaultCallMediaPlayer();
        } catch (Exception e) {
            showError("播放失败 " + e.getMessage());
        }
    }

    @Override
    protected void onPause() {
        super.onPause();
        if(ringtone != null && ringtone.isPlaying()){
            ringtone.stop();
        }
    }
}
